package com.PuzGame.combinenumbers.customViews;

import android.content.Context;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.widget.TextView;

import com.PuzGame.combinenumbers.R;
import com.PuzGame.combinenumbers.Interfaces.ITimerDone;

public class CountDown extends TextView {

	CountDownTimer timer;
	TextView tv;
	ITimerDone callback;
	
	public CountDown(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		Init(context);
	}
	public CountDown(Context context, AttributeSet attrs) {
		super(context, attrs);
		Init(context);
	}
	
	public CountDown(Context context, int seconds) {
		super(context);
		Init(context);
		
	}
	public void Init(Context context) {
		if(context instanceof ITimerDone)
			callback = (ITimerDone) context;
		tv = this;
		tv.setBackgroundResource(R.drawable.backtimers);
	}
	
	public void setBase(long diff) {
		timer = new CountDownTimer(diff, 1000) {
			
			public void onTick(long millis) {

				long second = (millis / 1000) % 60;
				long minute = (millis / (1000 * 60)) % 60;
				long hour = (millis / (1000 * 60 * 60)) % 24;
				long day = (millis / (1000 * 60 * 60 * 24));

				String time = "";
				if(day == 0 && hour == 0)
				{
					time = String.format("%02d:%02d", minute ,second);
				}
				else if(day == 0)
				{
					time = String.format("%02d:%02d:%02d", hour, minute ,second);
				}
				else
				{
					time = String.format("%01d:%02d:%02d:%02d", day, hour, minute, second);
				}

				tv.setText(time);
				
				if(millis/1000 < 10)
				{
					tv.setTextColor(Color.RED);
				}
				else if(millis/1000 < 60)
				{
					tv.setTextColor(Color.parseColor("#FFA500"));
				}
				else if(millis/1000 < 60*5)
				{
					tv.setTextColor(Color.YELLOW);
				}
				else if(millis/1000 < 60*10)
				{
					tv.setTextColor(Color.parseColor("#CCFF33"));
				}
				else
				{
					tv.setTextColor
					(Color.GREEN);
				}
			}

			public void onFinish() {
				if(callback != null)
					callback.TimerDone();
			}
		};
		
	}
	public void start()
	{
		if(timer!= null)
			timer.start();
	}
	
	public void stop()
	{
		if(timer!= null)
			timer.cancel();
	}

}
